package org.plr.ecommerce.view.pager;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.AbstractRepeater;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.string.JavascriptUtils;

public class Pager extends Panel {

    private Map<Integer, WebMarkupContainer> pages = new HashMap<Integer, WebMarkupContainer>();

    public Pager(String id) {
        super(id);

        setOutputMarkupId(true);

        Form form = new Form("form");

        form.add(new HiddenField("pageHidden", new Model(1), Integer.class));

        form.add(new AjaxButton("firstButton") {

            @Override
            protected void onSubmit(AjaxRequestTarget arg0, Form arg1) {
                HiddenField pageHidden = (HiddenField) Pager.this.get("form:pageHidden");
                System.out.println(pageHidden);
                System.out.println(pageHidden.getModelObject());
                pageHidden.setModelObject(1);
                onPageChanged();
                arg0.addComponent(Pager.this);
            }
        });

        form.add(new AjaxButton("prevButton") {

            @Override
            protected void onSubmit(AjaxRequestTarget arg0, Form arg1) {
                HiddenField pageHidden = (HiddenField) Pager.this.get("form:pageHidden");
                System.out.println(pageHidden);
                System.out.println(pageHidden.getModelObject());
                pageHidden.setModelObject((Integer) pageHidden.getModelObject() - 1);
                onPageChanged();
                arg0.addComponent(Pager.this);
            }
        });

        form.add(new AjaxButton("nextButton") {

            @Override
            protected void onSubmit(AjaxRequestTarget arg0, Form arg1) {
                HiddenField pageHidden = (HiddenField) Pager.this.get("form:pageHidden");
                System.out.println(pageHidden);
                System.out.println(pageHidden.getModelObject());
                pageHidden.setModelObject((Integer) pageHidden.getModelObject() + 1);
                onPageChanged();
                arg0.addComponent(Pager.this);
            }
        });

        form.add(new AjaxButton("lastButton") {

            @Override
            protected void onSubmit(AjaxRequestTarget arg0, Form arg1) {
                HiddenField pageHidden = (HiddenField) Pager.this.get("form:pageHidden");
                System.out.println(pageHidden);
                System.out.println(pageHidden.getModelObject());
                pageHidden.setModelObject(10);
                onPageChanged();
                arg0.addComponent(Pager.this);
            }
        });

        add(form);

        AbstractRepeater buffer = new AbstractRepeater("pages") {

            @Override
            protected Iterator renderIterator() {
                return pages.values().iterator();
            }

            @Override
            protected void onPopulate() {
                removeAll();
                for (WebMarkupContainer page : pages.values()) {
                    add(page);
                }
            }
        };

        add(buffer);


        add(new HeaderContributor(new IHeaderContributor() {

            public void renderHead(IHeaderResponse arg0) {
                arg0.renderString("<style type='text/css'></style>");
            }
        }));

        get("form:nextButton").add(new AttributeModifier("onclick", new Model("alert('coucou');")) {

            @Override
            protected String newValue(String currentValue, String replacementValue) {
                StringBuilder newValue = new StringBuilder();
                if (null != replacementValue) {
                    newValue.append("var styleSheet = document.styleSheets[0];");
                    newValue.append("if (styleSheet.cssRules) {");
                    newValue.append("styleSheet.insertRule('#page1{background-color: red;}', 0);");
                    newValue.append("} else {");
                    newValue.append("styleSheet.addRule('#page1', 'background-color: red;');");
                    newValue.append("}");
                }
                newValue.append(currentValue);
                return newValue.toString();
            }
        });

        onPageChanged();
    }

    protected void onPageChanged() {
        pages.clear();

        HiddenField pageHidden = (HiddenField) get("form:pageHidden");

        Integer currentPageNumber = (Integer) pageHidden.getModelObject();

        if (currentPageNumber < 1) {
            currentPageNumber = 1;
        }

        if (currentPageNumber > 10) {
            currentPageNumber = 10;
        }

        pageHidden.setModelObject(currentPageNumber);

        if (currentPageNumber > 1) {
            Integer prevPageNumber = currentPageNumber - 1;
            WebMarkupContainer prevPage = new WebMarkupContainer("page" + prevPageNumber, new Model(prevPageNumber));
            populatePage(prevPage);
            prevPage.setMarkupId("page" + prevPageNumber);
            prevPage.setOutputMarkupId(true);
            pages.put(prevPageNumber, prevPage);
        }

        if (currentPageNumber < 10) {
            Integer nextPageNumber = currentPageNumber + 1;
            WebMarkupContainer nextPage = new WebMarkupContainer("page" + nextPageNumber, new Model(nextPageNumber));
            populatePage(nextPage);
            nextPage.setMarkupId("page" + nextPageNumber);
            nextPage.setOutputMarkupId(true);
            pages.put(nextPageNumber, nextPage);
        }

        WebMarkupContainer currentPage = new WebMarkupContainer("page" + currentPageNumber, new Model(currentPageNumber));
        populatePage(currentPage);
        currentPage.setMarkupId("page" + currentPageNumber);
        currentPage.setOutputMarkupId(true);
        pages.put(currentPageNumber, currentPage);
    }

    public void populatePage(WebMarkupContainer page) {
        page.add(new Page("page", (Integer) page.getModelObject()));
    }
}
